﻿package ui;

import java.rmi.RemoteException;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import components.MyButton;
import components.MyFrame;
import components.MyPanel;

import vo.admin.LoginPanelVO;
import vo.admin.MainPanelVO;
import logicInterface.AdminLogicInterface;
import main.Main;
import adminLogic.MockAdminLogic;

public class MainPanel extends MyPanel{
	
	private MyButton logoutButton;
	private MyButton returnbackButton;
	
	private JLabel userlistLabel;
	private JLabel searchLabel;
	private DefaultTableModel dtm;
	private JTable userlist;
	private JScrollPane jsp;
	private JTextField searchField;
	private JLabel classLabel;
	private JComboBox classComboBox;
	private ButtonGroup buttonGroup;
	private JRadioButton identity_all;
	private JRadioButton identity_customer;
	private JRadioButton identity_GM;
	private JRadioButton identity_SM;
	private MyButton searchButton;
	private MyButton adduserButton;
	private MyButton deleteuserButton;
	private MyButton modifyuserButton;
	

	protected void initComponent() {
		try {
			Main.getLogic().searchUser();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		MainPanelVO mpvo=null;
		try {
			mpvo = Main.getLogic().getMpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		logoutButton = new MyButton("注销");
		returnbackButton = new MyButton("返回");
		logoutButton.setBounds(MyFrame.WIDTH-80,0, 80, 20);
		returnbackButton.setBounds(MyFrame.WIDTH-160,0, 80, 20);
		this.add(logoutButton);
		this.add(returnbackButton);
		
		userlistLabel = new JLabel("用户列表：");
		searchLabel = new JLabel("精确查找：");
		dtm = new DefaultTableModel(mpvo.getUserData(),new String[]{"用户名","身份"});
		userlist = new JTable(dtm);
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		userlist.setDefaultRenderer(Object.class, tcr);
		jsp = new JScrollPane(userlist);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		searchField = new JTextField();
		searchField.setText(mpvo.getKeyword());
		classLabel = new JLabel("类别：");
		classComboBox = new JComboBox(new String[]{"用户名"});
		classComboBox.setSelectedIndex(mpvo.getKeywordType());
		buttonGroup = new ButtonGroup();
		identity_all = new JRadioButton("全部");
		identity_customer = new JRadioButton("顾客");
		identity_GM = new JRadioButton("总经理");
		identity_SM = new JRadioButton("销售经理");
		switch(mpvo.getUserType()){
		case 0:
			identity_all.setSelected(true);
			break;
		case 1:
			identity_customer.setSelected(true);
			break;
		case 2:
			identity_GM.setSelected(true);
			break;
		case 3:
			identity_SM.setSelected(true);
			break;
		}
		searchButton = new MyButton("精确查找");
		adduserButton = new MyButton("增加用户");
		deleteuserButton = new MyButton("删除用户");
		modifyuserButton = new MyButton("修改信息");
		userlistLabel.setBounds(10, 20, 100, 20);
		searchLabel.setBounds(400, 20, 100, 20);
		jsp.setBounds(10, 40, 350, 300);
		searchField.setBounds(390, 50, 200, 25);
		classLabel.setBounds(390, 100, 80, 20);
		classComboBox.setBounds(490, 100, 100, 20);
		identity_all.setBounds(380, 140, 80, 20);
		identity_customer.setBounds(460, 140, 70, 20);
		identity_GM.setBounds(530, 140, 80, 20);
		identity_SM.setBounds(610, 140, 80, 20);
		searchButton.setBounds(480, 200, 100, 40);
		adduserButton.setBounds(385, 280, 90, 40);
		deleteuserButton.setBounds(485, 280, 90, 40);
		modifyuserButton.setBounds(585, 280,90, 40);
		this.add(jsp);
		this.add(userlistLabel);
		this.add(searchLabel);
		this.add(searchField);
		this.add(classLabel);
		this.add(classComboBox);
		buttonGroup.add(identity_all);
		buttonGroup.add(identity_customer);
		buttonGroup.add(identity_GM);
		buttonGroup.add(identity_SM);
		this.add(identity_all);
	    this.add(identity_customer);
     	this.add(identity_GM);
     	this.add(identity_SM);
		this.add(searchButton);
		this.add(adduserButton);
		this.add(deleteuserButton);
		this.add(modifyuserButton);		
		
		searchButton.setButtonAction("searchUser");
		adduserButton.setButtonAction("toAddUser");
		deleteuserButton.setButtonAction("deleteUser");
		modifyuserButton.setButtonAction("toModifyUser");
		logoutButton.setButtonAction("logout");
		returnbackButton.setButtonAction("logout");
	}

	protected void updateVO() {
		MainPanelVO mpvo=null;
		try {
			mpvo = Main.getLogic().getMpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		mpvo.setSelectedRow(userlist.getSelectedRow());
		int count=0;
		Enumeration<AbstractButton> enu = buttonGroup.getElements();
        while (enu.hasMoreElements()){		        	
       	    AbstractButton radioButton = enu.nextElement();
            if(radioButton.isSelected()){
            	break;
            }
            count++;
        }
        mpvo.setUserType(count);
        mpvo.setKeywordType(classComboBox.getSelectedIndex());
        mpvo.setKeyword(searchField.getText());
        Main.getLogic().setMpvo(mpvo);
	}



}
